// pages/visitingCardProduct/visitingCardProduct.js
const app = getApp()
const ctx = wx.createCanvasContext('visitingCardCanvas')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:[],
    touimg:'',
    winheight:0,
    flag:0,
    weixin: '',
    dizhi: '',
    phone: '',
    email: '',
    company:'',
    username:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)
    that.setData({
        winheight: wx.getSystemInfoSync().windowWidth*0.5
    })
    that.posterinfo(options);
  },
  /**
   * 绘制视图
   */
  drawPic(){
    var that = this
    var obj = that.data
    console.log(obj)
    let screenWidth = wx.getSystemInfoSync().windowWidth

      ctx.drawImage('/images/model2.png', 0, 0, screenWidth, screenWidth * 0.5)
      ctx.save()
      ctx.beginPath()
      ctx.arc(60, 90, 30, 0, 2 * Math.PI)
      ctx.closePath()
      ctx.clip()
      ctx.drawImage(obj.userIcon, 30, 60, 60, 60)
      ctx.restore()

      ctx.setTextAlign('left')
      ctx.setFillStyle("#424E75")
      ctx.setStrokeStyle('#424E75')
      ctx.setFontSize(15)
      if(obj.company!=''){
      ctx.fillText(obj.company, 10, 30)
      }
      ctx.setFontSize(18)
      ctx.setTextAlign('right')
      if(obj.username!=''){
      ctx.fillText(obj.username, 80, 140)
      }
      ctx.drawImage('/images/heit.png', 110,60, 2, 80)

      ctx.setTextAlign('left')
      ctx.setFillStyle("#424E75")
      ctx.setStrokeStyle('#424E75')
  
      if(obj.phone!=''){
        ctx.setFontSize(13)
        ctx.fillText('手机:' + obj.phone, 120, 80)
      }
      if(obj.weixin!=''){
           ctx.setFontSize(13)
           ctx.fillText('微信:' + obj.weixin, 120, 100)
      }
      if(obj.email!=''){
        ctx.setFontSize(13)
         ctx.fillText('邮箱:' + obj.email, 120, 120)
      }
      if(obj.dizhi!=''){
        if(obj.dizhi.length>11){
          var str=obj.dizhi;
          var bb=str.substring(0,11)
          var aa=str.substring(11)
          console.log(bb)
          console.log(aa)
              
          ctx.setFontSize(13)
          ctx.fillText('地址:'+bb, 120, 140)
          ctx.setFontSize(13)
          ctx.fillText(aa, 120, 160)
        }else{
          ctx.fillText('地址:'+obj.dizhi, 120, 140)
        }
      }
      // ctx.drawImage(obj.erma, screenWidth - 80, 100, 60, 60)

      ctx.draw()
  },
  posterinfo: function(e) {
     var that =this;
     wx.request({
          url: app.d.ceshiUrl + '/Api/Poster/poster',
          data: {
            uid:  e.id,
          },
          method: 'post', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            let rest = res.data;
             wx.downloadFile({
              url: rest.data.photo,
              success(res) {
                // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
                if (res.statusCode === 200) {
                    that.setData({
                        userIcon: res.tempFilePath,
                        flag1:1,
                    })
                    that.setData({
                       weixin: rest.data.name,
                       dizhi: rest.data.address,
                       phone: rest.data.phone,
                       email: rest.data.email,
                       company:rest.data.company,
                       username:rest.data.username
                    })
                     that.drawPic();
                }
              }
             })
          
          }
        })

  },
  /**
   * 保存名片
   */
  saveVisitingCard(){
    let screenWidth = wx.getSystemInfoSync().windowWidth
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: screenWidth,
      height: screenWidth * 0.5,
      destWidth: screenWidth * 2,
      destHeight: screenWidth * 0.5 * 2,
      canvasId: 'visitingCardCanvas',
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
        })
        wx.hideLoading()
        wx.showToast({
          title: '保存成功！',
          icon: 'success'
        })
        // setTimeout(function () {
        //   wx.navigateBack({
        //   })
        // }, 1000)
      }
    })
  },
  copyTBL1:function(e){
    var self=this;
    console.log(e)
    console.log('123')
    wx.setClipboardData({
    data: self.data.username,
    success: function(res) {
      console.log(res)
    }
  });
  },
  call: function () {
    var that=this;
    console.log(that.data)
    console.log(that.data.phone)
    wx.makePhoneCall({
      phoneNumber:that.data.phone,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  copyTBL6:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.dizhi,
    success: function(res) {

    }
  });
  },
  copyTBL3:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.weixin,
    success: function(res) {

    }
  });
  },
  copyTBL4:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.email,
    success: function(res) {

    }
  });
  },
  copyTBL5:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.company,
    success: function(res) {

    }
  });
  },
    copyTBL1:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.username,
    success: function(res) {

    }
  });
  },
  copyTBL1:function(e){
    var self=this;
    wx.setClipboardData({
    data: self.data.username,
    success: function(res) {

    }
  });
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})

